﻿#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QListView>
#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QWidget>
#include <qDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QStringListModel* model = new QStringListModel(this);
    model -> setStringList(QStringList() << "Apple" << "Banana" << "Cherry" << "Date" << "Elderberry");

    // 创建代理模型
    proxyModel = new QSortFilterProxyModel(this);
    proxyModel ->setSourceModel(model);

    // 创建QLineEdit用于输入搜索词
    QLineEdit *searchEdit = new QLineEdit(this);
    // 当文本改变时，更新代理模型的过滤器
    connect(searchEdit, &QLineEdit::textChanged, this, &Widget::filterList);
    // 创建ListView显示代理模型的内容
    QListView *listView = new QListView(this);
    listView->setModel(proxyModel);

    // 创建布局并设置主窗口
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(searchEdit);
    layout->addWidget(listView);
    searchEdit->setFocus();


}
void Widget::filterList(const QString &text) {
    // 获取QListView的模型
    proxyModel -> setFilterFixedString(text);
}
Widget::~Widget()
{
    delete ui;
}
